package com.cloudtasks;

import com.cloudtasks.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

/**
 * @author Administrator
 *
 */
public class AddItemBySearchingActivity extends Activity {
	
	String UserName = "";
	long UserID;
	String ListName = "";
	long ListID;
	final Context myContext = this;
	private Cursor DBcursor;
	final static String TYPE_PAGE = "type";
	private Database mDb;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_add_by_searching);
        
        try{          
        	Button addItemBB = (Button)findViewById(R.id.search_button);
            addItemBB.setOnClickListener(searchButtonListener); 
            
	        // Unwrap parameters from last activity: the User's name
	        // and also his User ID
        	Bundle param = getIntent().getExtras(); 
	        if(param != null){
	        	UserName = param.getString("uname");
		        //MyTools.displayToastMessage(myContext, UserName);
		        UserID = param.getLong("uid");
		        ListName = param.getString("");
		    	ListID = param.getLong("lid");
	        }
        	
	        // initialize the Database for this context
	        mDb = new Database(this);
	        mDb.open();	       
        }
        catch(Exception e){
        	MyTools.displayToastMessage(myContext, e.toString());
        }
    }
    
    private OnClickListener searchButtonListener = new OnClickListener(){
		public void onClick(View v){						
			try{
				EditText input = (EditText) findViewById(R.id.search_edittext);
				MyTools.displayToastMessage(myContext, input.getText().toString());
				
				ListView lv = (ListView) findViewById(R.id.search_results_listview);
		        DBcursor = mDb.fetchAllItems(input.getText().toString());
		        lv.setAdapter(new SimpleCursorAdapter(myContext,  
		        	    R.layout.single_item_searched,
		        	    DBcursor,
		        	    new String[] {Database.KEY_TYPE, Database.KEY_GROC_NAME},
		        	    new int[] { R.id.type_result_textview, R.id.item_result_textview}
		        	));
		        startManagingCursor(DBcursor);
		        TextView changetext = (TextView) findViewById(R.id.search_results_textview);
				changetext.setText("Found " + DBcursor.getCount() + " matches.");
		        
		        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
					@Override
					//All of this is not needed, just add item to list and return to whatever activity shows the user's list of types
					public void onItemClick(AdapterView<?> av, View view,
							int pos, long id) {
						String name = ((TextView)view.findViewById(R.id.item_result_textview)).getText().toString();
						String type = ((TextView)view.findViewById(R.id.type_result_textview)).getText().toString();

		        		mDb.createItem(name, ListID, false, type);

		        		MyTools.displayToastMessage(myContext, "Added " + name); 
					}  
		        });  
		        
			}
			catch(Exception e){
				MyTools.displayToastMessage(myContext,e.toString());	
			}
		}
	};
	public void onDestroy() {
        super.onDestroy();
     
        mDb.close();
    }
}
